
#include "PT100temp.h"
#include "stm32f10x.h"
#include "tm1638.h"
#include "rtc.h"
#include "beep.h"

PT100_Data_type Pt100Data[PT100Num] = {0};

Temp_threshold_type  tempThreshold[PT100Num] = {0};

unsigned int wucha = 0;             //误差偏移修正

unsigned int offset_alarm_temp = 5;  //告警偏移值

//AD采集值，请从低温到高温排
const signed int TEMP[87]= //温度值
{
  -200,-190,-180,-170,-160,-150,-140,-130,-120,-110,
  -100,-90 , -80, -70, -60, -50,-40, -30, -20, -10,
  0   , 10,  20,  30,  40,  50,  60,  70,  80,  90,
  100 , 110, 120, 130, 140, 150, 160, 170, 180, 190,
  200 , 210, 220, 230, 240, 250, 260, 270, 280, 290,
  300 , 310, 320, 330, 340, 350, 360, 370, 380, 390,
  400 , 410, 420, 430, 440, 450, 460, 470, 480, 490,
  500 , 510, 520, 530, 540, 550, 560, 570, 580, 590,
  600 , 610, 620, 630, 640, 650, 660
};
                                              
const unsigned int AD_R[87]  = //AD值   电阻值(Ω)
{
  1852 ,2283 ,2710 ,3134 ,3554 ,3972 ,4388 ,4800 ,5211 ,5619 ,
  6026 ,6430 ,6833 ,7233 ,7633 ,8031 ,8427 ,8822 ,9216 ,9609 ,
  10000,10390,10779,11167,11554,11940,12324,12708,13090,13471,
  13851,14229,14607,14983,15358,15733,16105,16477,16848,17217,
  17586,17953,18319,18684,19047,19410,19771,20131,20490,20848, 
  21205,21561,21915,22268,22621,22972,23321,23670,24018,24364, 
  24709,25053,25396,25738,26078,26418,26756,27093,27429,27764,
  28098,28430,28762,29092,29421,29749,30075,30401,30725,31049, 
  31371,31692,32012,32330,32648,32964,33279  
};

void PT100_Data_Init(void)
{
  memset(Pt100Data,0,sizeof(Pt100Data));
  
  flash_read(TEMP_THRESHOLD_ADDR,(uint8_t *)tempThreshold,TEMP_THRESHOLD_SIZE); //默认阈值
  
  Pt100Data[0].threshold = &tempThreshold[0];
  Pt100Data[1].threshold = &tempThreshold[1];
  Pt100Data[2].threshold = &tempThreshold[2];  
  Pt100Data[3].threshold = &tempThreshold[3]; 
}

void PT100_Alram_Level_event(void)
{
  static  alarm_byte_t alarm_t = {0};
  static uint8_t alrm_level_t[PT100Num] = {0};
  uint8_t num = 0;
  for(num = 0;num < PT100Num;num++)
  {
    led_Temp_alarm_show(num,Pt100Data[num].Level );                 //led指示灯
    setBitValue(alarm_t.Byte,num,Pt100Data[num].Level );
    //触发告警
    if( alrm_level_t[num] != Pt100Data[num].Level )
    {
      //保存告警记录信息
      add_Alarm_Record_RTC( 1,Pt100Data[num].Level,                //类型+状态
                            num+1,Pt100Data[num].TempValue/10 );   //端口编号+数值
            
      if( alarm_t.Byte == 0 )
      {
        //告警恢复
     #if BSP_BEEP_EN   
        Beep_clear();                     //关闭蜂鸣器
     #endif   
        Set_UI_Select(mainDisp);          //显示主界面
      }
      else
      {
        Set_UI_Select(CurrentAlarmDisp);        //显示报警数值
        Current_Alarm_MessageDisp(Pt100Data);   //lcd 显示报警数值
        #if BSP_BEEP_EN 
        //蜂鸣器报警
        switch( Pt100Data[num].Level)
        {
          case 1 :
            Beep_Activate(0xffff,10,50);//总时间+单次时间+间隔时间
          break;
         case 2 :
            Beep_Activate(0xffff,10,30);
          break;
         case 3 :
            Beep_Activate(0xffff,10,10);
          break;
        }
        #endif
      }

      alrm_level_t[num] = Pt100Data[num].Level;
    }
  }

}

void PT100_temp_event(uint16_t * Value)
{
  unsigned int IN = 0;                  //采集AD前清零
  unsigned int Temp = 0;                //温度值清零
  uint8_t num;
  int16_t TempValue[PT100Num] = {0};    //清除AD数据缓存
  memcpy(TempValue,Value,PT100Num*2);
  for( num = 0;num< PT100Num; num++)
  {
     //检测到没有插入pt100探头
     if( TempValue[num] >= 3975 ) {
        Pt100Data[num].TempValue = 0;  //温度精度 0.1 °c
        Pt100Data[num].Level = 0;      //正常
        break; 
     }
    
     Temp = 0;                    //温度值清零
//    IN = (TempValue[num] / 1000) + wucha;               //滤波
    IN = (TempValue[num]*32200/4096) + wucha;             //滤波

    if(IN < AD_R[0])       //温度超过测量范围，使用最后测量范围的K值进行计算
    {
      Temp = IN*10*TEMP[1]/AD_R[1];     //动态拟合比对，动态显示温度
    }
    else if(IN >= AD_R[0] && IN <= AD_R[86])  //温度在测量范围内，开始测温
    {
      unsigned int a=0;
      while(Temp == 0)                        //算出温度后跳出
      {
        if( IN >= AD_R[a] && IN <= AD_R[a+1]) //按区间比对
        {
          Temp = IN*10*TEMP[a]/AD_R[a];      //动态拟合比对，动态显示温度
        }
        a++;//AD采集区间切换
      }
    }
    else if(IN > AD_R[86])//温度超过测量范围，使用最后测量范围的K值进行计算
    {
      Temp = IN*10*TEMP[49]/AD_R[49];        //动态拟合比对，动态显示温度
    }
    Pt100Data[num].TempValue = Temp;         //温度精度 0.1 °c
    
    if( Temp/10 <  (Pt100Data[num].threshold->General - offset_alarm_temp ) )
    {
       Pt100Data[num].Level = 0x00;   //正常
    }
    else if( Temp/10 >= Pt100Data[num].threshold->General && Temp/10 <  Pt100Data[num].threshold->Great  )
    {
       Pt100Data[num].Level = 0x01;   //一般报警
    }
    else if( Temp/10 >= Pt100Data[num].threshold->Great && Temp/10 <  Pt100Data[num].threshold->Urgent  )
    {
       Pt100Data[num].Level = 0x02;   //重大报警
    }
    else if(Temp/10 >=  Pt100Data[num].threshold->Urgent ) {
       Pt100Data[num].Level = 0x03;   //紧急报警
    }
  }
//  PT100show_Temp(Temp);//温度显示
  
  PT100_Alram_Level_event();
}


